<?php

namespace Biz\service\advertisement;

class AdConfManagement
{
    const FIELD_ONCE = 1;
    const FIELD_START_ONCE            = 2;
    const FIELD_DISABLED_SHARE = 4;
    const FIELD_SHOW_TIME      = 8;
    const FIELD_SORT                  = 16;
    const FIELD_DISABLED_USER_PROFILE = 32;
    const FIELD_LINK                  = 64;
    const FIELD_DISPLAY = 128;
    const FIELD_CLOSE           = 256;
    const FIELD_DISABLED_REASON = 512;
    const FIELD_NOT_DISTURB = 1024;
    const FIELD_ONCE_DISTURB = 2048;
    const FIELD_DISABLED_AIM = 4096;
    const FIELD_ALL             = E_ALL;

    const REL_OPEN = 1;
    const REL_CLOSE = 2;
    const REL_MAP = [
        self::REL_OPEN => '开启',
        self::REL_CLOSE => '关闭',
    ];
    public static $commonField = 0;

    const FIELD_MAP = [
        self::FIELD_ONCE                  => '每天一次可见',
        self::FIELD_START_ONCE            => '每次启动一次可见',
        self::FIELD_CLOSE                 => '开启[关闭功能]',
        self::FIELD_SHOW_TIME             => '自定义[展示时间]',
        self::FIELD_SORT                  => '自定义[排序]',
        self::FIELD_DISABLED_SHARE        => '不使用[共享规则]',
        self::FIELD_DISPLAY               => '自定义[展示方式]',
        self::FIELD_LINK                  => '自定义[跳转链接]',
        self::FIELD_DISABLED_REASON       => '不使用[投放原因]',
        self::FIELD_DISABLED_AIM       => '不使用[投放目的]',
        self::FIELD_DISABLED_USER_PROFILE => '不使用[用户分群]',
        self::FIELD_NOT_DISTURB           => '新用户免打扰',
        self::FIELD_ONCE_DISTURB           => '新用户首次不可见',
    ];

    public static $ignore_show_conf = [
        self::FIELD_SORT,
        self::FIELD_SHOW_TIME,
        self::FIELD_DISPLAY,
        self::FIELD_LINK,
    ];

    public static function showConf($conf)
    {
        $rules = '';
        $config = self::formatField($conf);
        $html = [];
        foreach ($config as $v) {
            if (!in_array($v, self::$ignore_show_conf)) $html[] = AdConfManagement::FIELD_MAP[$v];
        }
        if ($html) $rules = '功能选择: ' . implode(' | ', $html);
        return $rules;
    }

    /**
     * @param int $field
     * @return int
     */
    public static function displayField($field)
    {
        return $field & self::$commonField;
    }

    public static function findField($field, $conf)
    {
        return $field & $conf;
    }

    public static function formatField($fields)
    {
        $response = [];
        $fields = intval($fields);
        foreach (self::FIELD_MAP as $field => $name) {
            if ($fields & $field) {
                $response[] = (string)$field;
            }
        }
        return $response;
    }

    public static function setConf($conf = self::FIELD_ALL)
    {
        self::$commonField = $conf;
    }
}